package com.fzfnn.scrapbook.service.impl;

import com.fzfnn.scrapbook.entity.Journal;
import com.fzfnn.scrapbook.mapper.SearchMapper;
import com.fzfnn.scrapbook.service.SearchService;
import com.fzfnn.scrapbook.util.R;
import com.fzfnn.scrapbook.vo.SearchUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchMapper searchMapper;

    @Autowired
    private RedisTemplate<String, String> redisMail;
    @Override
public List<Journal> searchJournals(String keyword) {
        return searchMapper.searchByTitle(keyword);
    }
    @Override
    public List<SearchUserVo> searchUser(String keyword) {
        if (keyword != null && !keyword.trim().isEmpty()) {
            return searchMapper.searchByUsernameOrEmail(keyword);
        }
        return new ArrayList<>(); // 关键词为空时返回空列表
    }
    @Override
    public boolean isUserFollowing(String token, Integer followedId) {
        String userId = redisMail.opsForValue().get("token:" + token);
        if (userId == null) {
            // 登录无效，默认未关注（或抛异常，由上层处理）
            return false;
        }
        return searchMapper.isFollowing(Integer.valueOf(userId), followedId) > 0;
    }


}
